package com.bw.module_video_detail.test_camera

import android.hardware.Camera
import android.os.Bundle
import android.view.SurfaceHolder
import android.view.SurfaceView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.bw.module_video_detail.R
import java.io.IOException

class CameraSurfaceViewActivity : AppCompatActivity(),SurfaceHolder.Callback {
    private lateinit var  surfaceView: SurfaceView
    private lateinit var surfaceHolder: SurfaceHolder
    private var mCamera: Camera? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_camera_surface_view)
        surfaceView = findViewById(R.id.surface_view)
        surfaceHolder = surfaceView.holder
        surfaceHolder.addCallback(this)

    }

    override fun surfaceCreated(holder: SurfaceHolder) {
        try {
            mCamera = Camera.open(1) // 0:后置摄像头 1:前置摄像头
            mCamera?.setPreviewDisplay(holder)
            mCamera?.setDisplayOrientation(90)
            mCamera?.startPreview()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {

    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {
        mCamera?.apply {
            stopPreview()
            release()
        }
        mCamera = null
    }
}